/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panel;

import builder.JadBuilder;
import builder.ApkBuilder;
import builder.JarBuilder;
import builder.DatBuilder;
import database.CompanyInfo;
import database.DataEngine;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import utils.Debug;
import utils.FileName;

/**
 *
 * @author daz
 */
public class BuildFilePanel extends Screen {
//    private Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 18)

    private JTextField pathField;
    private JButton openFileButton;
    private ActionListener openFileListenner;
    private JLabel cpNameLabel;
    private JComboBox cpNameBox;
    private JLabel cpIdLabel;
    private JTextField cpIdInputField;
    private JFileChooser fileChooser;
    private ApkBuilder apkBuilder;
    private DatBuilder datBuilder;
    private JarBuilder jarBuilder;
    private JadBuilder jadBuilder;
    private ActionListener buildListenner;
    private ActionListener buildManyListenner;
    private JLabel newFileNameLabel;
    private JTextField newFileNameInputField;
    private JButton buildButton;
    private JButton buildManyButton;
    private FileName fileObject;
    private List<CompanyInfo> listCompanyInfo;
    private ActionListener cpBoxActionListener;
    private String cpSelectedName;
    private int cpBoxX;
    private int cpBoxY;
    private int cpBoxHeight;
    private int cpBoxWidth;

    public BuildFilePanel(JFrame frame) {
        super(frame);
        initBuilder();

    }

    @Override
    void initValue() {
        canvasHeight = 300;
    }

    @Override
    void initComponent() {
        setLayout(null);
        int left = 10;
        int top = 20;
        int height = 20;
        int width = 100;
        int linePadding = 50;
        fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(
                fileChooser.getFileSystemView().getParentDirectory(
                new File("C:\\")));
        pathField = new JTextField("");
        openFileButton = new JButton("Chọn file");
        cpNameLabel = new JLabel("Tên CP :");
        cpIdInputField = new JTextField();
        newFileNameInputField = new JTextField();
        newFileNameLabel = new JLabel("Tên file mới:");
        buildButton = new JButton("Build file");
        cpIdLabel = new JLabel("Id :");
        buildManyButton = new JButton("Build nhiều bản");
        pathField.setBounds(left + 30, top, 250, height);
        //open file
        openFileButton.setBounds(cavanWidth - 100, top, width - 20, height);
        //cp line
        top += linePadding;
        cpNameLabel.setBounds(left + 30, top, 250, height);
        setCpList(left, top, height);
        cpIdLabel.setBounds(left + 270, top, 30, height);
        cpIdInputField.setBounds(left + 300, top, 50, height);
        cpIdInputField.setEditable(false);
        top += linePadding;
        newFileNameLabel.setBounds(left + 30, top, 250, height);
        newFileNameInputField.setBounds(left + 100, top, 150, height);
        top += linePadding;
        buildButton.setBounds(left + 30, top, width - 20, height);
        top += linePadding;
        buildManyButton.setBounds(left + 30, top, width + 50, height);
        buildButton.addActionListener(buildListenner);
        openFileButton.addActionListener(openFileListenner);
        buildManyButton.addActionListener(buildManyListenner);
//        add(buildManyButton);
        add(pathField);
        add(cpIdLabel);
        add(cpIdInputField);
        add(newFileNameLabel);
        add(newFileNameInputField);
        add(buildButton);
        add(openFileButton);
        add(cpNameLabel);

    }

    private void initBuilder() {
        apkBuilder = new ApkBuilder();
        datBuilder = new DatBuilder();
        jarBuilder = new JarBuilder();
        jadBuilder = new JadBuilder();
    }

    public void setCpList(int left, int top, int height) {
        listCompanyInfo = DataEngine.getInstance().getCpInfo();
        String[] companyName = new String[listCompanyInfo.size()];
        for (int i = 0; i < listCompanyInfo.size(); i++) {
            companyName[i] = listCompanyInfo.get(i).getName();
        }
        cpNameBox = new JComboBox(companyName);
        cpNameBox.addActionListener(cpBoxActionListener);
        cpNameBox.setSelectedIndex(0);
        cpIdInputField.setText(String.valueOf(listCompanyInfo.get(0).getCompanyId()));
        cpNameBox.setBounds(left + 100, top, 150, height);
        this.cpBoxX = left + 100;
        this.cpBoxY = top;
        this.cpBoxWidth = 150;
        this.cpBoxHeight = height;
        add(cpNameBox);
    }

    public void setCpList() {
        cpNameBox.removeAllItems();
        listCompanyInfo = DataEngine.getInstance().getCpInfo();
        String[] companyName = new String[listCompanyInfo.size()];
        for (int i = 0; i < listCompanyInfo.size(); i++) {
            companyName[i] = listCompanyInfo.get(i).getName();
            cpNameBox.addItem(companyName[i]);
        }

    }

    @Override
    void initListenner() {
        openFileListenner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                int returnVal = fileChooser.showOpenDialog(BuildFilePanel.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    try {
                        fileObject = new FileName(file.getAbsolutePath(), '/', '.');
                        Debug.d("extension :" + fileObject.extension());
                        String foldePath = file.getParent();
                        String fileName = file.getName();
                        datBuilder.setFolderPath(foldePath);
                        if (fileObject.extension().equals("jar")) {
                            jarBuilder.setPathFolder(foldePath);
                            jarBuilder.setJarName(fileName);
                            jadBuilder.setFolderPath(foldePath);
                        } else if (fileObject.extension().equals("apk")) {
                            apkBuilder.setPathFolder(foldePath);
                            apkBuilder.setApkName(fileName);
                        }

                        System.out.println("test :" + file.getParent());
                    } catch (Exception ex) {
                        Logger.getLogger(BuildFileScreen.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    pathField.setText(file.getAbsolutePath());
                } else {
                }
            }
        };
        buildListenner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (fileObject.extension().equals("jar")) {
                    jarBuilder.setNewJarName(newFileNameInputField.getText());
                    jadBuilder.setNewFileName(newFileNameInputField.getText());
                    datBuilder.creatDatFile(cpSelectedName, cpIdInputField.getText());
                    jarBuilder.creatFile();
                    jadBuilder.creatDatFile();
                    JOptionPane.showMessageDialog(frame,
                            "Tạo file thành công.");
                } else if (fileObject.extension().equals("apk")) {
                    datBuilder.creatDatFile(cpSelectedName, cpIdInputField.getText());
                    apkBuilder.setNewApkName(newFileNameInputField.getText());
                    apkBuilder.createFile();
                    JOptionPane.showMessageDialog(frame,
                            "Tạo file thành công.");
                }
            }
        };
        cpBoxActionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox) e.getSource();
                int index = cb.getSelectedIndex();
                Debug.d("index :" + index);
                if (index >= 0) {
                    cpSelectedName = listCompanyInfo.get(index).getName();
                    Debug.d("Id :" + listCompanyInfo.get(index).getCompanyId());
                    Debug.d(String.valueOf(listCompanyInfo.get(index).getCompanyId()));
                    cpIdInputField.setText(String.valueOf(listCompanyInfo.get(index).getCompanyId()));
                }
            }
        };
        buildManyListenner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < 10; i++) {
                    if (fileObject.extension().equals("jar")) {
                        jarBuilder.setNewJarName(newFileNameInputField.getText() + (i + 1));
                        jadBuilder.setNewFileName(newFileNameInputField.getText() + (i + 1));
                        int id = Integer.parseInt(cpIdInputField.getText()) + i;
                        datBuilder.creatDatFile(cpSelectedName, String.valueOf(id));
                        jarBuilder.creatFile();
                        jadBuilder.creatDatFile();

                    } else if (fileObject.extension().equals("apk")) {
                        int id = Integer.parseInt(cpIdInputField.getText()) + i;
                        datBuilder.creatDatFile(cpSelectedName, String.valueOf(id));
                        apkBuilder.setNewApkName(newFileNameInputField.getText() + (i + 1));
                        apkBuilder.createFile();
                    }
                }
                JOptionPane.showMessageDialog(frame,
                        "Tạo file thành công.");
            }
        };
    }
}
